﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Data;
using System.Data.Entity;
using System.IO;
using WmWebSystem.Models;
using WmWebSystem.Service;

namespace WmWebSystem.DbHelper
{
    public static class ProjectHelper
    {
        public static void CreateNewToProjects(string pno, string title, int type, string summary, decimal target, string logo,string delpath)
        {
            WmDb mydb = new WmDb();

            DateTime time = DateTime.Now;
            string author = HttpContext.Current.User.Identity.Name;
            decimal currentValue = 0;
            int attentionNum = 0;
            string editor = "default";

            Project pro = new Project();
            pro.ProjectNo = pno;
            pro.ProjectName = title;
            pro.Content = summary;
            pro.Type = type;
            pro.Author = author;
            pro.TargetValue = target;
            pro.CurrentValue = currentValue;
            pro.Logo = logo;
            pro.DelPath = delpath;
            pro.AddTime = time;
            pro.Status = 0;
            pro.Editor = editor;
            pro.AttentionNum = attentionNum;
            try
            {
                mydb.Projects.Add(pro);
                mydb.SaveChanges();
                //将记录写入日志
                string p1 = "Add new to Projects, The ProjectNo:" + pno;
                WriteLog.WriteSysLog(p1);
            }
            catch (FieldAccessException ex)
            {
                //失败，进行回滚操作  记录异常
                File.Delete(logo);
                WriteLog.WriteSysLog(ex.ToString());
            }

        }

        public static void DeleteFromProjects(int id)
        {
            WmDb mydb = new WmDb();
            var pro = mydb.Projects.Find(id);
            string p_no = pro.ProjectNo;

            //删除项目
            try
            {
                mydb.Projects.Remove(pro);
                mydb.SaveChanges();
                //将记录写入日志
                string p1 = "Delete a project from Projects, where projectId:" + id;
                WriteLog.WriteSysLog(p1);
            }
            catch (FieldAccessException ex)
            {
                //失败，进行回滚操作  记录异常

                WriteLog.WriteSysLog(ex.ToString());
            }

        }

        public static Project FindProjectByProjectNo(string pno)
        {
            WmDb mydb = new WmDb();
            var pro = (from p in mydb.Projects
                       where p.ProjectNo == pno
                       select p).FirstOrDefault();
            return pro;
        }

        public static IEnumerable<Project> FindProjectBySearch(string pno, DateTime stime, DateTime etime, int status)
        {
            WmDb mydb = new WmDb();
            if (pno == null && stime == null && etime == null && status == 0)
            {
                //0000
                var projects = from p in mydb.Projects
                               orderby p.AddTime descending
                               select p;
                return projects;
            }
            else if (pno != null && stime == null && etime == null && status == 0)
            {
                //1000
                var projects = from p in mydb.Projects
                           where p.ProjectNo.Contains(pno)
                           select p;
                return projects;
            }
            else if (pno == null && stime != null && etime == null && status == 0)
            {
                //0100
                DateTime time = Convert.ToDateTime(stime);
                var projects = from p in mydb.Projects
                           where p.AddTime >= time
                           select p;
                return projects;
            }
            else if (pno == null && stime == null && etime != null && status == 0)
            {
                //0010
                DateTime time = Convert.ToDateTime(etime);
                var projects = from p in mydb.Projects
                           where p.AddTime <= time
                           select p;
                return projects;
            }
            else if (pno == null && stime == null && etime == null && status != 0) 
            {
                //0001
                var projects = from p in mydb.Projects
                           where p.Status == status
                           select p;
                return projects;
            }
            else if (pno != null && stime != null && etime == null && status == 0)
            {
                //1100
                DateTime time = Convert.ToDateTime(stime);
                var projects = from p in mydb.Projects
                           where p.ProjectNo.Contains(pno) && p.AddTime>=time
                           select p;
                return projects;
            }
            else if (pno != null && stime == null && etime != null && status == 0)
            {
                //1010
                DateTime time = Convert.ToDateTime(etime);
                var projects = from p in mydb.Projects
                           where p.ProjectNo.Contains(pno) && p.AddTime <= time
                           select p;
                return projects;
            }
            else if (pno != null && stime == null && etime == null && status != 0)
            {
                //1001
                var projects = from p in mydb.Projects
                           where p.ProjectNo.Contains(pno) && p.Status == status
                           select p;
                return projects;
            }
            else if (pno == null && stime != null && etime != null && status == 0)
            {
                //0110
                DateTime startTime = Convert.ToDateTime(stime);
                DateTime endTime = Convert.ToDateTime(etime);
                var projects = from p in mydb.Projects
                           where p.AddTime >= startTime && p.AddTime <= endTime
                           select p;
                return projects;
            }
            else if (pno == null && stime != null && etime == null && status != 0)
            {
                //0101
                DateTime startTime = Convert.ToDateTime(stime);
                var projects = from p in mydb.Projects
                           where p.AddTime >= startTime && p.Status == status
                           select p;
                return projects;
            }
            else if (pno == null && stime == null && etime != null && status != 0)
            {
                //0011
                DateTime endTime = Convert.ToDateTime(etime);
                var projects = from p in mydb.Projects
                           where p.Status == status && p.AddTime <= endTime
                           select p;
                return projects;
            }
            else if (pno == null && stime != null && etime != null && status != 0)
            {
                //0111
                DateTime startTime = Convert.ToDateTime(stime);
                DateTime endTime = Convert.ToDateTime(etime);
                var projects = from p in mydb.Projects
                           where p.AddTime >= startTime && p.AddTime <= endTime && p.Status == status
                           select p;
                return projects;
            }
            else if (pno != null && stime == null && etime != null && status != 0)
            {
                //1011
                DateTime endTime = Convert.ToDateTime(etime);
                var projects = from p in mydb.Projects
                           where p.ProjectNo.Contains(pno) && p.AddTime <= endTime && p.Status == status
                           select p;
                return projects;
            }
            else if (pno != null && stime != null && etime == null && status != 0)
            {
                //1101
                DateTime startTime = Convert.ToDateTime(stime);
                var projects = from p in mydb.Projects
                           where p.ProjectNo.Contains(pno) && p.AddTime >= startTime && p.Status == status
                           select p;
                return projects;
            }
            else if (pno != null && stime != null && etime != null && status == 0)
            {
                //1110
                DateTime startTime = Convert.ToDateTime(stime);
                DateTime endTime = Convert.ToDateTime(etime);
                var projects = from p in mydb.Projects
                           where p.ProjectNo.Contains(pno) && p.AddTime >= startTime && p.AddTime <= endTime
                           select p;
                return projects;
            }
            else 
            {
                //1111
                DateTime startTime = Convert.ToDateTime(stime);
                DateTime endTime = Convert.ToDateTime(etime);
                var projects = from p in mydb.Projects
                           where p.ProjectNo.Contains(pno) && p.AddTime >= startTime && p.AddTime <= endTime && p.Status == status
                           select p;
                return projects;
            }
            
        }

        public static IEnumerable<Project> FindPrpjectByUserName(string user) 
        {
            WmDb mydb = new WmDb();
            var pro = from p in mydb.Projects
                      where p.Author == user
                      select p;
            return pro;
        }

        public static Project FindProjectById(int id)
        {
            WmDb mydb = new WmDb();
            var pro = mydb.Projects.Find(id);
            return pro;
        }

        public static IEnumerable<Project> FindTopEightProjectsByStatus(int status)
        {
            WmDb mydb = new WmDb();
            var pro = (from p in mydb.Projects
                       where p.Status == status && p.CurrentValue < p.TargetValue
                       orderby p.AddTime descending
                       select p).Take(8);
            return pro;
        }

        public static IEnumerable<Project> FindAllProjectByStatus(int status)
        {
            WmDb mydb = new WmDb();
            var project = from p in mydb.Projects
                          where p.Status == status
                          orderby p.AddTime descending
                          select p;
            return project;
        }

        public static IEnumerable<Project> FindAll()
        {
            WmDb mydb = new WmDb();
            var project = from p in mydb.Projects
                          orderby p.AddTime descending
                          select p;
            return project;
        }

        public static int GetAttentionHelperProjectCount(string pno, string user)
        {
            WmDb mydb = new WmDb();
            int count = 0;
            if (user != null)
            {
                count = (from a in mydb.Attentions
                         where a.ProjectNo == pno && a.UserName == user
                         select a).Count();
            }
            else
            {
                count = (from a in mydb.Attentions
                         where a.ProjectNo == pno
                         select a).Count();
            }
            return count;
        }

        public static bool ChangeTheStatusOfThisProject(string pno, int status)
        {
            WmDb mydb = new WmDb();
            var project = (from p in mydb.Projects
                           where p.ProjectNo == pno
                           select p).FirstOrDefault();
            project.Status = status;
            
            mydb.SaveChanges();
            return true;

        }

        //当该文件夹不存在时，创建该文件夹
        public static string CreateNewFolderInProjectContent(string pno)
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + "\\ProjectContent\\" + pno;

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
                return path;
            }
            else
            {
                return path;
            }
        }

        public static bool HasArriveTheTarget(string pno)
        {
            var project = FindProjectByProjectNo(pno);
            if (project.CurrentValue == project.TargetValue)
                return true;
            else
                return false;
        }



    }
}
